jQuery(function($){
	var toolbar={};
	var order={
			grid : null,
			init:function(){
				var items = [];		
				var exportExcel = [];
				exportExcel = [{text: "新增多渠道订单",
					id:"export", 
					click: function(){
						order.exportExcel();
					}, icon: "add"},{ line: true }];
				items = items.concat(exportExcel);
				toolbar = {items : items};
				$("#btnSearch").on("click",function(){
					//order.pageList();
					order.grid.set({
						parms: [
								{name: "receiverName", value:$("#receiverName").val()},
								{name: "receiverPhone", value:$("#receiverPhone").val()},
								{name: "deliveryState", value:$("#deliveryState").val()},
								{name: "channelOrderId", value:$("#channelOrderId").val()},
								{name: "channelOrderType", value:$("#channelOrderType").val()},
								{name: "payState", value:$("#payState").val()},
								{name: "payType", value:$("#payType").val()},
								{name: "orderStartTime", value:$("#orderStartTime").val()},
								{name: "orderEndTime", value:$("#orderEndTime").val()},
								{name: "returnOrder", value:$("#returnOrder").val()}
			                    ],
						url: "channelOrderPageList.do"      
					});
					//order.grid.loadData();					
				});
				this.pageList();
				this.channelOrderType=[
				    {value:0,text:"京东"},
				    {value:1,text:"淘宝"},
				    {value:2,text:"天猫"}
				];
				this.orderState=[
					{value:0,text:"已提交"},
					{value:1,text:"审核通过"},
					{value:2,text:"审核不通过"},
					{value:3,text:"待支付"},
					{value:4,text:"待发货"},
					{value:5,text:"已发货"},
					{value:6,text:"已完成"},
					{value:7,text:"已取消"},
					{value:8,text:"已锁定"},
					{value:9,text:"部分发货"},
					{value:10,text:"已收货"},
					{value:11,text:"已关闭"},
					{value:12,text:"已生成包裹单"},
					{value:13,text:"已生成出库单"}
				];
				this.deliveryState=[
				    {value:0,text:"未发货"},
				    {value:1,text:"已发货"}              
				];
				this.payState=[
				    {value:0,text:"未支付"},
				    {value:1,text:"已支付"}              
				];
				this.payType=[
				    {value:1,text:"货到付款"},
				    {value:2,text:"邮局汇款"},
				    {value:3,text:"自提"},
				    {value:4,text:"在线支付"},   
				    {value:5,text:"公司转账"},
				    {value:6,text:"银行卡转账"} 
				];
				this.orderType=[
					{value:0,text:"白拿"},
					{value:1,text:"返还"},
					{value:2,text:"天猫"}
				];
				this.setSelectBox(this.payState,$("#payState"));
				this.setSelectBox(this.payType,$("#payType"));
				this.setSelectBox(this.channelOrderType,$("#channelOrderType"));
				this.setSelectBox(this.deliveryState,$("#deliveryState"));
			},
			pageList:function(){
				order.grid = $("#maingrid").ligerGrid({
		            columns: [
		            { display: '订单编号', name: 'channelOrderId',width:100},
		            { display: '订单渠道', name: 'channelOrderType',width:80,
		              render: function (rowData, rowindex, value){
		            	    return order.getTextByValue(order.channelOrderType,value);
				    }},
				    { display: '订单状态', name: 'orderState', width:100,
			          render: function (rowData, rowindex, value){
			            	return order.getTextByValue(order.orderState,value);
			        }},
			        { display: '收货人/电话', name: 'receiver', width:150,
				          render: function (rowData, rowindex, value){
				            	return rowData.receiverName+"/"+rowData.receiverPhone
				    }},
		            /*{ display: '发货状态', name: 'deliveryState', width:80,
		              render: function (rowData, rowindex, value){
		            	    return order.getTextByValue(order.deliveryState,value);
		            }},*/
		            { display: '支付状态', name: 'payState',width:80,
		            	render: function (rowData, rowindex, value){
		            		return order.getTextByValue(order.payState,value);
			        }},
		            { display: '支付方式', name: 'payType',width:80,
		            	render: function (rowData, rowindex, value){
		            		if(value){
		            			//value=value.substring(0,1);
		            			return order.getTextByValue(order.payType,value);
		            		}
		            		return "";
				    }},
		            /*{ display: '订单类型', name: 'orderType',width:80,
		            	render: function (rowData, rowindex, value){
		            		return order.getTextByValue(order.orderType,value);
				    }},*/
				    { display: '订单商品', name: 'orderItems',width:280,align:"left",
		            	render: function (rowData, rowindex, items){
		            		var result="";
	    					for(var index in items){
	    						var skuName="";
	    						if(items[index].skuName!=null && items[index].skuName!="" && items[index].skuName.length>20){
	    							skuName=items[index].skuName.substring(0,20)+"...";
	    						}else{
	    							skuName=items[index].skuName;
	    						}
	    						if(index>0){
	    							result=result+"&nbsp;<br />";
	    						}
	    						result=result+skuName;
	    					}
		            		return result;
				    }},
				    { display: '商品数量', name: 'orderItems',width:60,align:"center",
		            	render: function (rowData, rowindex, items){
		            		var result="";
	    					for(var index in items){
	    						if(index>0){
	    							result=result+"<br />";
	    						}
	    						result=result+items[index].itemTotal;
	    					}
		            		return result;
				    }},
		            { display: '总金额(元)', name: 'orderTotalPrice',width:80},
		            { display: '下单时间',width:'160', name: 'orderStartTime',width:150},
		            { display: '买家备注', name: 'orderRemark',width:280,align:"left",render: function (rowData, rowindex, value){
		            	if(value!=null && value!="" && value.length>20){
							value=value.substring(0,20)+"...";
						}
	            		return value;
		            }},
		            { display: '操作',width:'20%', isSort: false,width:200, render: function (rowdata, rowindex, value){
		            	var create_product_package = "<a href='javascript:createProductPackage(" + rowindex + ",1)'>生成包裹单</a> ";
		            	var h = "<a href='javascript:showCommodityInfo(" + rowindex + ",1)'>订单信息</a> ";
		            	for ( var i in roles) {
		            		//商品专员,商品主管,管理员
		               		if(roles[i] == 'GOOD_PERSON' || roles[i] == 'SYSTEM_ADMIN' || roles[i] == 'GOODS_SUPERVISOR'){
		               			var css = "";
			             		if(rowdata.checkState == 1) css = "style='color: gray;'";
			             		h += "<a href='javascript:setCommodityInfo(" + rowindex + ",2)' "+css+">设置供应商</a> ";
			             	    if(rowdata.checkState != 1){
			                    	h += "<a href='javascript:updateCommodity(" + rowindex + ")' >修改</a> ";
			                    }
			                    h += "<a href='javascript:delCommodity(" + rowindex + ")' "+css+">删除</a> ";
		               		}
		        		}
		            	
		             	if(rowdata.orderState == 12 || rowdata.orderState == 13 || rowdata.orderState == 5){
		             		return h;
		             	}
		             	
		             	if(!rowdata.returnOrder || rowdata.returnOrder == '0'){
			             	if(rowdata.payState==1 || rowdata.orderType==0){
			             		return create_product_package+h;
			             	}
		             	}
		            
		             		return h;
		            	}
		            }
		            ], 
		            pageSize:30,
		            width: '100%',
		            height: '100%',
		            dataType: 'server', 
		            rowHeight:'auto',
		            rownumbers:false,
		            dataAction: 'server',
		            pagesizeParmName: 'pageSize',
		            parms: [
							{name: "channelOrderId", value:$("#channelOrderId").val()},
							{name: "channelOrderType", value:$("#channelOrderType").val()},
							{name: "payState", value:$("#payState").val()},
							{name: "payType", value:$("#payType").val()},
							{name: "orderStartTime", value:$("#orderStartTime").val()},
							{name: "orderEndTime", value:$("#orderEndTime").val()},
							{name: "returnOrder", value:$("#returnOrder").val()}
		                    ],
					url: "channelOrderPageList.do",
					toolbar: toolbar
		        });
				grid=order.grid;
			},
			setSelectBox:function(objs,dom){
				if(objs  && objs.length){
					dom.html('<option value="">请选择</option>');
					for(var x in objs){
						dom.append('<option value="'+objs[x].value+'">'+objs[x].text+'</option>');
					}
				}
			},
			getTextByValue:function(objs,value){
				if(objs && objs.length){
					for(var x in objs){
						if(objs[x].value==value){
							return objs[x].text;
						}
					}
				}
			},
			exportExcel:function(){
				var url='../../../order/channelOrder/to_Channel_Order.do';
	        	url+="?pTabid="+WIN_TAB.getSelectedTabItemID();
	        	window.parent.f_addTab('新增多渠道订单','新增多渠道订单',url);
			}
	};
	order.init();
}); 